package com.cskaoyan.service.front.comment;

import com.cskaoyan.bean.bo.front.Comment.CommentSendBO;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.front.comment.CommentCountVO;
import com.cskaoyan.bean.vo.front.comment.CommentInUserInfoVO;
import com.cskaoyan.bean.vo.front.comment.CommentSendVo;
import com.cskaoyan.bean.vo.front.comment.CommentVO;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @author: jzc
 * @since: 2022/10/15 14:40
 **/
@Service
public class WxCommentServiceImpl implements WxCommentService {

    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Autowired
    MarketUserMapper marketUserMapper;

    @Override
    public CommonData getCommentList(BasePageInfo basePageInfo, Integer valueId, Byte type, Boolean showType) {
        ArrayList<CommentVO> commentVOS = new ArrayList<>();
        CommonData<CommentVO> commonData = new CommonData<>();
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        marketCommentExample.setOrderByClause("add_time desc");
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId);

        criteria.andTypeEqualTo(type);
        if (showType == true) {
            criteria.andHasPictureEqualTo(showType);
        }

        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);
        PageInfo<MarketComment> marketCommentPageInfo = new PageInfo<>(marketComments);
        for (MarketComment marketComment : marketComments) {
            Integer userId = marketComment.getUserId();
            MarketUser marketUser = marketUserMapper.selectByPrimaryKey(userId);
            CommentVO commentVO = new CommentVO(
                    new CommentInUserInfoVO(marketUser.getNickname(), marketUser.getAvatar()),
                    new Date(),
                    marketComment.getPicUrls(),
                    marketComment.getAdminContent(),
                    marketComment.getContent()
            );
            commentVOS.add(commentVO);
        }
        MarketCommentExample commentExample = new MarketCommentExample();
        MarketCommentExample.Criteria commentExampleCriteria = commentExample.createCriteria();
        commentExampleCriteria.andValueIdEqualTo(valueId);
        long count = marketCommentMapper.countByExample(commentExample);
        commonData.setTotal((int) count);
        commonData.setPages(marketCommentPageInfo.getPages());
        commonData.setLimit(basePageInfo.getLimit());
        commonData.setPage(basePageInfo.getPage());
        commonData.setList(commentVOS);
        return commonData;
    }

    @Override
    public CommentCountVO getCommentCount(Integer valueId, Byte type) {
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(type);
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);
        int hasPicCount = 0;
        for (MarketComment marketComment : marketComments) {
            hasPicCount = marketComment.getPicUrls().length;
        }
        CommentCountVO commentCountVO = new CommentCountVO(
                hasPicCount,
                marketComments.size()
        );
        return commentCountVO;
    }

    @Override
    public CommentSendVo SendComment(CommentSendBO commentSendBO, MarketUser marketUser) {
        Date date = new Date();
        MarketComment marketComment = new MarketComment(
                null,
                commentSendBO.getValueId(),
                commentSendBO.getType(),
                commentSendBO.getContent(),
                null,
                marketUser.getId(),
                commentSendBO.getHasPicture(),
                commentSendBO.getPicUrls(),
                commentSendBO.getStar(),
                date,
                date,
                false
        );

        marketCommentMapper.insertSelective(marketComment);
        CommentSendVo commentSendVo = new CommentSendVo(
                marketComment.getId(),
                marketComment.getValueId(),
                marketComment.getType(),
                marketComment.getContent(),
                marketComment.getUserId(),
                marketComment.getHasPicture(),
                marketComment.getPicUrls(),
                marketComment.getStar(),
                marketComment.getAddTime(),
                marketComment.getUpdateTime()
        );
        return commentSendVo;

    }
}
